package Leo.serviceClient.controller;

import Leo.serviceClient.ribbon.SecondLoadbalanceRule;
import Leo.serviceClient.ribbon.SecondPing;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
public class FaqClientController {

    @Autowired
    RestTemplate restTemplate; //spring-web的REST客户端

    @Autowired
    DiscoveryClient discoveryClient;  //发现服务器，用于向服务器获取provider的实例

    @Autowired
    SpringClientFactory springClientFactory; //Ribbon的LoadBalancer管理器

//    @Autowired
//    SecondLoadbalanceRule secondLoadbalanceRule;
//
//    @Autowired
//    SecondPing secondPing;
    /**
     * 客户端调用provider的方法
     * @param id
     * @return
     */
    @RequestMapping("/faqQuery/{id}")
    public String query(@PathVariable("id") String id){
        if(id == null || "".equals(id)){
            id = "ojbk";
        }
        String url = "http://FAQ-Service-Provider/query/" + id;
        String forObject = restTemplate.getForObject(url, String.class);
        return forObject;
    }

    @RequestMapping("/demo")
    public String demo(){
        String forObject = restTemplate.getForObject("http://FAQ-Service-Provider/test", String.class);
        return forObject;
    }

    @RequestMapping("/demo2")
    public String demo2(){
        String forObject = restTemplate.getForObject("http://Second-Provider/test", String.class);
        return forObject;
    }

    @RequestMapping("/setDefault")
    public String setDefault(){
        ILoadBalancer loadBalancer = springClientFactory.getLoadBalancer("default");
        ILoadBalancer loadBalancer3 = springClientFactory.getLoadBalancer("Second-Provider");
        System.out.println("default loadbalance 等于 second balance？："+loadBalancer.equals(loadBalancer3));
        ILoadBalancer loadBalancer2 = springClientFactory.getLoadBalancer("FAQ-Service-Provider");
        ZoneAwareLoadBalancer loadBalancer1 = (ZoneAwareLoadBalancer) loadBalancer;
        System.out.println("default rule is :" + loadBalancer1.getRule().getClass().getName());
//        loadBalancer1.setRule(new SecondLoadbalanceRule());
//        System.out.println("new rule is :" + loadBalancer1.getRule().getClass().getName());
        System.out.println("default ping is: " + loadBalancer1.getPing().getClass().getName());
//        loadBalancer1.setPing(new SecondPing());
//        System.out.println("new ping is: " + loadBalancer1.getPing().getClass().getName());
        System.out.println("_________________________________");
        loadBalancer1= (ZoneAwareLoadBalancer)loadBalancer3;
        System.out.println("second rule is :" + loadBalancer1.getRule().getClass().getName());
        System.out.println("second ping is: " + loadBalancer1.getPing().getClass().getName());

        System.out.println("_________________________________");
        loadBalancer1= (ZoneAwareLoadBalancer)loadBalancer2;
        System.out.println("FAQ rule is :" + loadBalancer1.getRule().getClass().getName());
        System.out.println("FAQ ping is: " + loadBalancer1.getPing().getClass().getName());
        String forObject = restTemplate.getForObject("http://Second-Provider/test", String.class);
        return forObject;
    }

    /**
     * 读取yml文件的metadata信息
     * @return
     */
    @RequestMapping("/metadata")
    public String metadata(){
        List<ServiceInstance> instances = discoveryClient.getInstances("FAQ-Service-Provider");
        StringBuilder stringBuilder = new StringBuilder();

        for (ServiceInstance instance : instances) {
            stringBuilder.append("service id :").append(instance.getServiceId());
            Map<String, String> metadata = instance.getMetadata();
            stringBuilder.append("\nremark:" ).append(metadata.get("remark")).append("\n");
        }
        String toString = stringBuilder.toString();
        System.out.println(toString);
        return toString;
    }

    @RequestMapping("/metadata2")
    public String metadata2(){
        List<ServiceInstance> instances = discoveryClient.getInstances("Second-Provider");
        StringBuilder stringBuilder = new StringBuilder();

        for (ServiceInstance instance : instances) {
            stringBuilder.append("service id :").append(instance.getServiceId());
            Map<String, String> metadata = instance.getMetadata();
            stringBuilder.append("\nremark:" ).append(metadata.get("remark")).append("\n");
        }
        String toString = stringBuilder.toString();
        System.out.println(toString);
        return toString;
    }
}
